package com.muh.service.impl;

import com.muh.entity.Employee;
import com.muh.repository.EmployeeRepository;
import com.muh.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeRepository employeeRepository;

    @Override
    public Employee save(Employee employee) {
        return employeeRepository.save(employee);
    }

    @Override
    public Object saveAll(List<Employee> employees) {
        return employeeRepository.saveAll(employees);
    }

    @Override
    public Employee findById(Long id) {
        return employeeRepository.findById(id).get();
    }

    @Override
    public List<Employee> findAll() {
        Iterator<Employee> iterator = employeeRepository.findAll().iterator();

        List<Employee> employeeList = new ArrayList<Employee>();
        iterator.forEachRemaining(employeeList::add);

//        Iterable<Employee> iterable = () -> iterator;
//        List<Employee> actualList = StreamSupport
//                .stream(iterable.spliterator(), false)
//                .collect(Collectors.toList());

        return employeeList;
    }

    @Override
    public List<Employee> findEmployeesByAddress(String address) {
        List<Employee> byTitle = employeeRepository.findEmployeesByAddress(address);
        List<Employee> employeesByName = employeeRepository.findEmployeesByName(address);
        return byTitle;
    }

    @Override
    public Page<Employee> findAllPage(int pageNumber, int pageSize) {
//        long count = employeeRepository.count();
        Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.Direction.DESC,"age");
        Page<Employee> all = employeeRepository.findAll(pageable);
        return all;
    }


    @Override
    public Page<Employee> findPageByAddress(String address, int pageNumber, int pageSize) {
        Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.Direction.DESC,"age");
//        Page<Employee> employees = employeeRepository.findAll( pageable);
        Page<Employee> employeesByAddress = employeeRepository.findEmployeesByAddress(address, pageable);
//        Page<Employee> employeeByDescription = employeeRepository.findEmployeeByDescription(address, pageable);
//        Page<Employee> employeeByRemark = employeeRepository.findEmployeeByRemark(address, pageable);
//        Page<Employee> employeesByName = employeeRepository.findEmployeesByName(address, pageable);

        return employeesByAddress;
    }

    @Override
    public boolean deleteDocumentById(Long id) {
        try {
            employeeRepository.deleteById(id);
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteAll() {
        try {
            employeeRepository.deleteAll();
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Employee update(Employee employee) {
        Employee save = employeeRepository.save(employee);
        return save;
    }

}
